import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.Color;
import javax.swing.SwingConstants;

/**
 * 
 * @author Matt Lee
 *
 */
public class DeleteUserPanel extends JPanel {
	private JTextField txtUsername;

	private JTextField txtAdminUsername;
	
	private JPasswordField adminPassword;
	
	private JButton btnNewButton;
	
	User currentUser;
	JComboBox comboBox;
	private JLabel lblResult;
	
	private class buttonListener implements ActionListener{

		@SuppressWarnings("deprecation")
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(txtAdminUsername.getText().equals(currentUser.username) 
					&& adminPassword.getText().equals(currentUser.password)){
				int searchType;
				if(comboBox.getSelectedIndex() == 0) searchType = UserDatabase.PATIENT;
				else if(comboBox.getSelectedIndex() == 1) searchType = UserDatabase.NURSE;
				else if(comboBox.getSelectedIndex() == 2) searchType = UserDatabase.DOCTOR;
				else searchType = UserDatabase.SYSTEM_ADMIN;
				String result = UserDatabase.removeUser(txtUsername.getText(), searchType);
				lblResult.setText(result);
				DataSaveLoad.save();
			}
			else{
				lblResult.setText("Wrong admin credentials");
			}
			
		}
		
		
	}
	
	public DeleteUserPanel() {
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);
		
		JLabel label = new JLabel("Delete User:");
		springLayout.putConstraint(SpringLayout.NORTH, 
				label, 10, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, label, 159, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, label, -159, SpringLayout.EAST, this);
		label.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
		add(label);
		
		txtUsername = new JTextField();
		add(txtUsername);
		txtUsername.setColumns(10);
		
		JLabel label_1 = new JLabel("System Adminstator Authorization:");
		springLayout.putConstraint(SpringLayout.NORTH, label_1, 26, SpringLayout.SOUTH, txtUsername);
		springLayout.putConstraint(SpringLayout.WEST, 
				label_1, 64, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, 
				label_1, -80, SpringLayout.EAST, this);
		label_1.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
		add(label_1);
		
		txtAdminUsername = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, txtUsername, 0, SpringLayout.WEST, txtAdminUsername);
		springLayout.putConstraint(SpringLayout.NORTH, 
				txtAdminUsername, 32, SpringLayout.SOUTH, label_1);
		springLayout.putConstraint(SpringLayout.WEST, 
				txtAdminUsername, 125, SpringLayout.WEST, this);
		add(txtAdminUsername);
		txtAdminUsername.setColumns(10);
		
		adminPassword = new JPasswordField("");
		springLayout.putConstraint(SpringLayout.NORTH, 
				adminPassword, 19, SpringLayout.SOUTH, txtAdminUsername);
		springLayout.putConstraint(SpringLayout.EAST, 
				txtAdminUsername, 0, SpringLayout.EAST, adminPassword);
		springLayout.putConstraint(SpringLayout.WEST, 
				adminPassword, 125, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, 
				adminPassword, 285, SpringLayout.WEST, this);
		add(adminPassword);
		adminPassword.setColumns(10);
		
		btnNewButton = new JButton("Delete User");
		springLayout.putConstraint(SpringLayout.NORTH, 
				btnNewButton, 51, SpringLayout.SOUTH, label_1);
		springLayout.putConstraint(SpringLayout.EAST, 
				btnNewButton, -30, SpringLayout.EAST, this);
		add(btnNewButton);
		btnNewButton.addActionListener(new buttonListener());
		
		JLabel lblEnterTheUsername = new JLabel("Enter the Username " +
				"of the User you want to delete");
		springLayout.putConstraint(SpringLayout.NORTH, txtUsername, 28, SpringLayout.SOUTH, lblEnterTheUsername);
		springLayout.putConstraint(SpringLayout.NORTH, 
				lblEnterTheUsername, 6, SpringLayout.SOUTH, label);
		springLayout.putConstraint(SpringLayout.WEST, 
				lblEnterTheUsername, 64, SpringLayout.WEST, this);
		add(lblEnterTheUsername);
		
		JLabel lblUsername = new JLabel("Username:");
		springLayout.putConstraint(SpringLayout.NORTH, lblUsername, 3, SpringLayout.NORTH, txtUsername);
		springLayout.putConstraint(SpringLayout.EAST, lblUsername, -6, SpringLayout.WEST, txtUsername);
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblUsername);
		
		JLabel lblAdminUsername = new JLabel("Admin Username:");
		springLayout.putConstraint(SpringLayout.NORTH, lblAdminUsername, 3, SpringLayout.NORTH, txtAdminUsername);
		springLayout.putConstraint(SpringLayout.EAST, lblAdminUsername, -6, SpringLayout.WEST, txtAdminUsername);
		lblAdminUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblAdminUsername);
		
		JLabel lblAdminPassword = new JLabel("Admin Password:");
		springLayout.putConstraint(SpringLayout.NORTH, lblAdminPassword, 3, SpringLayout.NORTH, adminPassword);
		springLayout.putConstraint(SpringLayout.EAST, lblAdminPassword, -6, SpringLayout.WEST, adminPassword);
		lblAdminPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblAdminPassword);
		
		comboBox = new JComboBox();
		springLayout.putConstraint(SpringLayout.NORTH, comboBox, 0, SpringLayout.NORTH, txtUsername);
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Patient", "Nurse", "Doctor", "Admin"}));
		comboBox.setSelectedIndex(0);
		add(comboBox);
		
		JLabel lblUsertype = new JLabel("Usertype:");
		springLayout.putConstraint(SpringLayout.WEST, comboBox, 15, SpringLayout.EAST, lblUsertype);
		springLayout.putConstraint(SpringLayout.NORTH, lblUsertype, 3, SpringLayout.NORTH, txtUsername);
		springLayout.putConstraint(SpringLayout.WEST, lblUsertype, 6, SpringLayout.EAST, txtUsername);
		lblUsertype.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblUsertype);
		
		lblResult = new JLabel("");
		lblResult.setFont(new Font("Tahoma", Font.ITALIC, 11));
		springLayout.putConstraint(SpringLayout.NORTH, lblResult, 24, SpringLayout.SOUTH, adminPassword);
		springLayout.putConstraint(SpringLayout.WEST, lblResult, 19, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, lblResult, -5, SpringLayout.EAST, txtAdminUsername);
		lblResult.setHorizontalAlignment(SwingConstants.CENTER);
		lblResult.setForeground(Color.RED);
		add(lblResult);
	}
	public JButton getBtnNewButton() {
		return btnNewButton;
	}
	public JComboBox getComboBox() {
		return comboBox;
	}
	public JLabel getLblResult() {
		return lblResult;
	}
	
	public String toString(){
		return "deleteUser";
	}
}
